package com.patrol.logisticsmanage.ui.orders;

import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.patrol.logisticsmanage.R;
import com.patrol.logisticsmanage.net.BaseObserver;
import com.patrol.logisticsmanage.net.BaseReponse;
import com.patrol.logisticsmanage.net.BaseRequest;
import com.patrol.logisticsmanage.ui.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

@Route(path = "/app/orderDetails")
public class OrderDetailsActivity extends BaseActivity {

    @Autowired
    String outid;

    @BindView(R.id.reltotal)
    TextView tvReltotal;
    @BindView(R.id.danbztotal)
    TextView tvDanbztotal;
    @BindView(R.id.zhiftotal)
    TextView tvZhiftotal;
    @BindView(R.id.tanshhwtotal)
    TextView tvTanshhwtotal;
    @BindView(R.id.dangctotal)
    TextView tvDangctotal;
    @BindView(R.id.natotal)
    TextView tvNatotal;
    @BindView(R.id.gaitotal)
    TextView tvGaitotal;

    @BindView(R.id.rcy_order_details)
    RecyclerView rcyOrderDetails;

    OrderDetailAdapter orderDetailAdapter;
    List<OrderDetail> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details);
        ButterKnife.bind(this);

        rcyOrderDetails.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));

        orderDetailAdapter = new OrderDetailAdapter(mContext);
        orderDetailAdapter.showEmptyView(true);

        orderDetailAdapter.setList(list);

        rcyOrderDetails.setAdapter(orderDetailAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }
    private Handler handler = new Handler();
    public void getData(){
        new Thread(()->{
            try {
                List<OrderDetail> result = BaseRequest.getInstance().getApiServise().xiaofjlxq(outid)
                        .execute().body().getResults();
                list.clear();
                list.addAll(result);

                Double rel =0d;// Double.parseDouble(detail.getRel());
                Double danbz =0d;//= Double.parseDouble(detail.getDanbz());
                Double zhif =0d;//= Double.parseDouble(detail.getZhif());
                Double tanshhw =0d;//= Double.parseDouble(detail.getTanshhw());
                Double dangc =0d;//= Double.parseDouble(detail.getDangc());
                Double na =0d;//= Double.parseDouble(detail.getNa());
                Double gai =0d;//= Double.parseDouble(detail.getGai());
                for(OrderDetail detail:list){
                    rel += Double.parseDouble(null == detail.getRel()|| "".equals(detail.getRel())?"0":detail.getRel());
                    danbz += Double.parseDouble(null == detail.getDanbz()|| "".equals(detail.getDanbz())?"0":detail.getDanbz());
                    zhif += Double.parseDouble(null == detail.getZhif()|| "".equals(detail.getZhif())?"0":detail.getZhif());
                    tanshhw += Double.parseDouble(null == detail.getTanshhw()|| "".equals(detail.getTanshhw())?"0":detail.getTanshhw());
                    dangc += Double.parseDouble(null == detail.getDangc()|| "".equals(detail.getDangc())?"0":detail.getDangc());
                    na += Double.parseDouble(null == detail.getNa()|| "".equals(detail.getNa())?"0":detail.getNa());
                    gai += Double.parseDouble(null == detail.getGai()|| "".equals(detail.getGai())?"0":detail.getGai());
                }
                Double finalRel = rel;
                Double finalDanbz = danbz;
                Double finalZhif = zhif;
                Double finalTanshhw = tanshhw;
                Double finalDangc = dangc;
                Double finalNa = na;
                Double finalGai = gai;
                handler.post(()->{
                    orderDetailAdapter.notifyDataSetChanged();
                    tvReltotal.setText("热量(千卡):" + String.format("%.2f", finalRel));
                    tvDanbztotal.setText("蛋白质(克):" + String.format("%.2f", finalDanbz));
                    tvZhiftotal.setText("脂肪(克):"+String.format("%.2f", finalZhif));
                    tvTanshhwtotal.setText("碳水化物(克):" + String.format("%.2f", finalTanshhw));
                    tvDangctotal.setText("胆固醇(毫克):" + String.format("%.2f", finalDangc));
                    tvNatotal.setText("钠(毫克):" + String.format("%.2f", finalNa));
                    tvGaitotal.setText("钙(毫克):" + String.format("%.2f", finalGai));
                });

            }catch (Exception ex){
                ex.printStackTrace();
            }
        }).start();
//        BaseRequest.getInstance().getApiServise().xiaofjlxq(outid)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new BaseObserver<List<OrderDetail>>(mContext) {
//                    @Override
//                    public void onSuccess(BaseReponse<List<OrderDetail>> baseReponse) {
//                        List<OrderDetail> result =  baseReponse.getResults();
//                        list.clear();
//                        list.addAll(result);
//                        orderDetailAdapter.notifyDataSetChanged();
//
//                        Double rel =0d;// Double.parseDouble(detail.getRel());
//                        Double danbz =0d;//= Double.parseDouble(detail.getDanbz());
//                        Double zhif =0d;//= Double.parseDouble(detail.getZhif());
//                        Double tanshhw =0d;//= Double.parseDouble(detail.getTanshhw());
//                        Double dangc =0d;//= Double.parseDouble(detail.getDangc());
//                        Double na =0d;//= Double.parseDouble(detail.getNa());
//                        Double gai =0d;//= Double.parseDouble(detail.getGai());
//                        for(OrderDetail detail:list){
//                            rel += Double.parseDouble(null == detail.getRel()|| "".equals(detail.getRel())?"0":detail.getRel());
//                            danbz += Double.parseDouble(null == detail.getDanbz()|| "".equals(detail.getDanbz())?"0":detail.getDanbz());
//                            zhif += Double.parseDouble(null == detail.getZhif()|| "".equals(detail.getZhif())?"0":detail.getZhif());
//                            tanshhw += Double.parseDouble(null == detail.getTanshhw()|| "".equals(detail.getTanshhw())?"0":detail.getTanshhw());
//                            dangc += Double.parseDouble(null == detail.getDangc()|| "".equals(detail.getDangc())?"0":detail.getDangc());
//                            na += Double.parseDouble(null == detail.getNa()|| "".equals(detail.getNa())?"0":detail.getNa());
//                            gai += Double.parseDouble(null == detail.getGai()|| "".equals(detail.getGai())?"0":detail.getGai());
//                        }
//                        tvReltotal.setText("热量(千卡):" + String.format("%.2f", rel));
//                        tvDanbztotal.setText("蛋白质(克):" + String.format("%.2f", danbz));
//                        tvZhiftotal.setText("脂肪(克):"+String.format("%.2f", zhif));
//                        tvTanshhwtotal.setText("碳水化物(克):" + String.format("%.2f", tanshhw));
//                        tvDangctotal.setText("胆固醇(毫克):" + String.format("%.2f", dangc));
//                        tvNatotal.setText("钠(毫克):" + String.format("%.2f", na));
//                        tvGaitotal.setText("钙(毫克):" + String.format("%.2f", gai));
//                    }
//
//                    @Override
//                    public void onCodeError(BaseReponse tBaseReponse) {
//                        tBaseReponse.getCode();
//                    }
//                    @Override
//                    public void onFailure(Throwable e, boolean netWork) throws Exception {
//                        e.printStackTrace();
//                    }
//                });
    }
}
